package com.aliengen.thingple.driverapp.service;

import android.os.AsyncTask;

import com.activeandroid.query.From;
import com.activeandroid.query.Select;
import com.aliengen.thingple.driverapp.data.local.EventModel;
import com.aliengen.thingple.driverapp.network.api.ThingpleAPI;
import com.aliengen.thingple.driverapp.network.api.exception.APICallException;

import org.json.JSONObject;

import java.util.List;

/**
 * This class is part of the Thingple mobile application for the CMC168 Handheld reader.
 *
 * Copyright AlienGen Limited 2016. All rights reserved.
 *
 * @author Timothé Mermet-Buffet <tim@aliengen.com>
 */
public class AsyncUpdater extends AsyncTask<EventModel, Integer, Integer> {
    protected OnExecuteListener mExecuteListener = null;
    protected HandsetApplication mApplication;

    public interface OnExecuteListener {
        public void onPreExecute();
        public void onPostExecute();
        public void onProgressUpdate();
        public void onMessage(String message);
    }

    @Override
    protected Integer doInBackground(EventModel... list) {
        List<EventModel> events = new Select().from(EventModel.class).orderBy("timestamp ASC").execute();

        int ret = events.size();
        if(ret == 0)
        {
            return ret;
        }

        for(final EventModel event : events) {
            if (event.operation_id == 10) // Anomaly
            {
                try {
                    mApplication.api.addAnomaly(event, new ThingpleAPI.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject object) {
                            event.delete();
                        }

                        @Override
                        public void onError(String message) {
                            if (mExecuteListener != null) {
                                mExecuteListener.onMessage(message);
                            }
                        }
                    });
                } catch (APICallException e) {
                    if (mExecuteListener != null) {
                        mExecuteListener.onMessage(e.getMessage());
                    }
                }
            }
            else {
                try {
                    mApplication.api.updateCylinder(event, new ThingpleAPI.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject object) {
                            event.delete();
                        }

                        @Override
                        public void onError(String message) {
                            if (mExecuteListener != null) {
                                mExecuteListener.onMessage(message);
                            }
                        }
                    });
                } catch (APICallException e) {
                    if (mExecuteListener != null) {
                        mExecuteListener.onMessage(e.getMessage());
                    }
                }
            }
        }

        return ret;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        if(mExecuteListener != null)
        {
            mExecuteListener.onPreExecute();
        }
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
        if(mExecuteListener != null)
        {
            mExecuteListener.onProgressUpdate();
        }
    }

    @Override
    protected void onPostExecute(Integer o) {
        super.onPostExecute(o);

        if(mExecuteListener != null)
        {
            mExecuteListener.onPostExecute();
        }
    }

    public void setOnExecuteListener(OnExecuteListener listener)
    {
        this.mExecuteListener = listener;
    }

    public void setApplication(HandsetApplication mApplication)
    {
        this.mApplication = mApplication;
    }
}
